package test230606;

/**
 * @author 兴趣使然黄小黄
 * @version 1.0
 * @date 2023/6/6 20:15
 * 206. 反转链表
 * https://leetcode.cn/problems/reverse-linked-list/
 */
public class Solution02 {

    // 三指针法
    public ListNode reverseList(ListNode head) {
        // 边界处理
        if (head == null || head.next == null) {
            return head;
        }
        // 三指针
        ListNode pre = null;
        ListNode cur = head;
        // 迭代反转
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = pre;
            pre = cur;
            cur = curNext;
        }
        return pre;
    }

    private class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }
}
